package com.hjx.movie.core.system.controller.admin;

import com.hjx.movie.common.utils.Result;
import com.hjx.movie.common.utils.ResultType;
import com.hjx.movie.core.system.service.admin.ICinemaService;
import com.hjx.movie.core.system.service.commons.file.FileService;
import com.hjx.movie.core.system.vo.FileVo;
import com.hjx.movie.persistence.system.model.IvyCinema;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>Title:</p>
 * <p>Description:</p>
 *
 * @author hjx
 * @date 2021/2/8 - 16:32
 */
@Slf4j
@RestController
@RequestMapping("/cinema")
public class CinemaController {
    @Resource
    ICinemaService cinemaService;
    @Resource
    FileService fileService;

    @GetMapping("/findAll")
    public Result findAllCinemaData(){
        return Result.success(cinemaService.findAllCinema());
    }

    @GetMapping("/findByCondition")
    public Result findCinemaByAreaId(@RequestParam(name = "areaId")Long areaId,
                                     @RequestParam(name = "cinemaNameLike",required = false)String cinemaNameLike,
                                     @RequestParam(name = "cinemaStatus",required = false)Integer cinemaStatus){
        return Result.success(cinemaService.findCinemaByCondition(areaId, cinemaNameLike, cinemaStatus));
    }

    @PostMapping("/add")
    public Result addCinema(@RequestBody IvyCinema cinema){
        if (cinemaService.addCinema(cinema)){
            return Result.success("新增影院成功");
        }
        return Result.error(ResultType.USER_INPUT_ERROR,"新增影院失败");
    }
    @GetMapping("/delete")
    public Result deleteCinema(@RequestParam("cinemaId") Long cinemaId){
        if (cinemaService.realDeleteCinema(cinemaId)){
            return Result.success("删除影院成功");
        }
        return Result.error(ResultType.USER_INPUT_ERROR,"删除影院失败");
    }
    @GetMapping("/logicDelete")
    public Result logicDeleteCinema(@RequestParam("cinemaId") Long cinemaId){
        if (cinemaService.logicDeleteCinema(cinemaId)){
            return Result.success("删除影院成功");
        }
        return Result.error(ResultType.USER_INPUT_ERROR,"删除影院失败");
    }


    @PostMapping("/update")
    public Result updateCinema(@RequestBody IvyCinema cinema){
        if (cinemaService.updateCinema(cinema)){
            return Result.success("修改影院成功");
        }
        return Result.error(ResultType.USER_INPUT_ERROR,"修改影院失败");
    }

    @RequestMapping("cinemaUpload")
    public Result upload(@RequestHeader("cinemaId")Long cinemaId, MultipartFile file){
        String realPath = fileService.getRealPath(cinemaId,"FOLDER_CINEMA_PIC");
        log.debug("realPath:{}",realPath);
        String name = FileService.fileUpload(file,realPath);
        String baseUrl = fileService.getBaseResHttpUrl("BASE_URL_CINEMA_PIC");
        String url = baseUrl+cinemaId+"/"+name;
        FileVo f = FileVo.builder().name(name).status("done").url(url).thumbUrl(url).build();
        return Result.success(f);
    }

    @RequestMapping("cinemaResList")
    public Result movieResList(@RequestParam("cinemaId") Long cinemaId){
        String realPath = fileService.getRealPath(cinemaId,"FOLDER_CINEMA_PIC");
        log.debug("realPath:{}",realPath);
        List<FileVo> picFileList = fileService.getResFileList(cinemaId,realPath,"BASE_URL_CINEMA_PIC");
        return Result.success(picFileList);
    }
    @RequestMapping("deleteCinemaRes")
    public Result deleteMovieRes(@RequestParam("cinemaId") Long cinemaId,@RequestParam("fileName")String fileName,@RequestParam("type")String type){
        String realPath = fileService.getRealPath(cinemaId,"FOLDER_CINEMA_PIC");
        log.debug("realPath:{}",realPath);
        FileService.deleteRes(fileName,realPath);
        return Result.success("删除影院照片成功");
    }
}
